package org.flower.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author Aizen
 */
@WebFilter("/manager/*")
public class AdminLoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        String uri = req.getRequestURI();
        if(uri.contains("/adminLogin.jsp")||uri.contains("/adminLoginServlet")||
                uri.contains("/css/")||uri.contains("/js/")||uri.contains("/image/")){
            chain.doFilter(request,response);
        }else{
            Object adminUser = req.getSession().getAttribute("adminUser");
            if(adminUser==null||adminUser.equals("")){
                req.getRequestDispatcher("adminLogin.jsp").forward(request,response);
            }else{
                chain.doFilter(request,response);
            }
        }

    }

    @Override
    public void destroy() {
    }
}
